package org.example.book.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.example.book.bean.Book;
import org.example.book.common.R;
import org.example.book.dto.BookDTO;
import org.example.book.service.BookService;
import org.example.book.vo.RespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 猪头
 */
@Tag(name = "图书管理")
@CrossOrigin
@RestController
public class BookController {
    @Autowired
    BookService bookService;

    //增
    @PostMapping("/book/add")
    @Operation(summary = "添加图书")
    public R add(@RequestBody @Valid BookDTO bookDTO) {
        Book book = new Book();
        BeanUtils.copyProperties(bookDTO, book);
        bookService.add(book);
        return R.success();
    }

    //改
    @PutMapping("/book/update")
    @Operation(summary = "更新图书")
    public R update(@RequestBody @Valid BookDTO bookDTO) {
        Book book = new Book();
        BeanUtils.copyProperties(bookDTO, book);
        bookService.update(book);
        return R.success();
    }

    //删
    @DeleteMapping("/book/{id}")
    @Operation(summary = "删除图书")
    public R delete(@PathVariable("id") Integer id) {
        bookService.delete(id);
        return R.success();
    }

    //查单
    @Parameters(value = {@Parameter(name = "id", description = "图书id", in = ParameterIn.PATH)})
    @GetMapping("/book/{id}")
    @Operation(summary = "查询图书")
    public R queryById(@PathVariable("id") Integer id) {
        Book book = bookService.queryById(id);
        RespVO respVO = new RespVO();
        BeanUtils.copyProperties(book, respVO);
        return R.success(book);
    }

    //查所有
    @GetMapping("/books")
    @Operation(summary = "查询所有图书")
    public R queryAll() {
      List<Book> books =  bookService.queryAll();
      List<RespVO> respVOS = books.stream()
              .map(book -> {
          RespVO respVO = new RespVO();
          BeanUtils.copyProperties(book, respVO);
          return respVO;
      }).collect(Collectors.toList());
      return R.success(respVOS);
    }
    // 在 BookController.java 中添加以下方法
@GetMapping("/books/price-range")
@Operation(summary = "按价格区间查询图书")
public R queryBooksByPriceRange(
        @Parameter(description = "最小价格") @RequestParam Double minPrice,
        @Parameter(description = "最大价格") @RequestParam Double maxPrice) {
    List<Book> books = bookService.queryBooksByPriceRange(minPrice, maxPrice);
    List<RespVO> respVOS = books.stream()
            .map(book -> {
                RespVO respVO = new RespVO();
                BeanUtils.copyProperties(book, respVO);
                return respVO;
            }).collect(Collectors.toList());
    return R.success(respVOS);
}

@GetMapping("/books/price-ranges")
@Operation(summary = "查询所有价格区间的图书")
public R queryBooksInAllPriceRanges() {
    Map<String, List<RespVO>> result = new HashMap<>();

    // 0-30区间
    List<Book> books0To30 = bookService.queryBooksByPriceRange(0.0, 30.0);
    List<RespVO> respVOS0To30 = books0To30.stream().map(book -> {
        RespVO respVO = new RespVO();
        BeanUtils.copyProperties(book, respVO);
        return respVO;
    }).collect(Collectors.toList());
    result.put("0-30", respVOS0To30);

    // 30-60区间
    List<Book> books30To60 = bookService.queryBooksByPriceRange(30.0, 60.0);
    List<RespVO> respVOS30To60 = books30To60.stream().map(book -> {
        RespVO respVO = new RespVO();
        BeanUtils.copyProperties(book, respVO);
        return respVO;
    }).collect(Collectors.toList());
    result.put("30-60", respVOS30To60);

    // 60以上区间
    List<Book> booksAbove60 = bookService.queryBooksByPriceRange(60.0, null);
    List<RespVO> respVOSAbove60 = booksAbove60.stream().map(book -> {
        RespVO respVO = new RespVO();
        BeanUtils.copyProperties(book, respVO);
        return respVO;
    }).collect(Collectors.toList());
    result.put("60+", respVOSAbove60);

    return R.success(result);
}


}
